package ms.unicorn.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ms.unicorn.Diagram;
import ms.unicorn.Element;
import ms.unicorn.Entity;
import ms.unicorn.Relation;
import ms.unicorn.UnicornPackage;
import ms.unicorn.diagram.edit.parts.DiagramEditPart;
import ms.unicorn.diagram.edit.parts.EntityEditPart;
import ms.unicorn.diagram.edit.parts.RelationEditPart;
import ms.unicorn.diagram.providers.UnicornElementTypes;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

/**
 * @generated
 */
public class UnicornDiagramUpdater {

	/**
	 * @generated
	 */
	public static List<UnicornNodeDescriptor> getSemanticChildren(View view) {
		switch (UnicornVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_1000SemanticChildren(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<UnicornNodeDescriptor> getDiagram_1000SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<UnicornNodeDescriptor> result = new LinkedList<UnicornNodeDescriptor>();
		for (Iterator<?> it = modelElement.getElements().iterator(); it
				.hasNext();) {
			Element childElement = (Element) it.next();
			int visualID = UnicornVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == EntityEditPart.VISUAL_ID) {
				result.add(new UnicornNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getContainedLinks(View view) {
		switch (UnicornVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_1000ContainedLinks(view);
		case EntityEditPart.VISUAL_ID:
			return getEntity_2001ContainedLinks(view);
		case RelationEditPart.VISUAL_ID:
			return getRelation_4001ContainedLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getIncomingLinks(View view) {
		switch (UnicornVisualIDRegistry.getVisualID(view)) {
		case EntityEditPart.VISUAL_ID:
			return getEntity_2001IncomingLinks(view);
		case RelationEditPart.VISUAL_ID:
			return getRelation_4001IncomingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getOutgoingLinks(View view) {
		switch (UnicornVisualIDRegistry.getVisualID(view)) {
		case EntityEditPart.VISUAL_ID:
			return getEntity_2001OutgoingLinks(view);
		case RelationEditPart.VISUAL_ID:
			return getRelation_4001OutgoingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getDiagram_1000ContainedLinks(
			View view) {
		Diagram modelElement = (Diagram) view.getElement();
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Relation_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getEntity_2001ContainedLinks(
			View view) {
		Entity modelElement = (Entity) view.getElement();
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Relation_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getRelation_4001ContainedLinks(
			View view) {
		Relation modelElement = (Relation) view.getElement();
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_Relation_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getEntity_2001IncomingLinks(
			View view) {
		Entity modelElement = (Entity) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_Relation_4001(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getRelation_4001IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getEntity_2001OutgoingLinks(
			View view) {
		Entity modelElement = (Entity) view.getElement();
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		result.addAll(getOutgoingTypeModelFacetLinks_Relation_4001(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<UnicornLinkDescriptor> getRelation_4001OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	private static Collection<UnicornLinkDescriptor> getContainedTypeModelFacetLinks_Relation_4001(
			Element container) {
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		for (Iterator<?> links = container.getElements().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Relation) {
				continue;
			}
			Relation link = (Relation) linkObject;
			if (RelationEditPart.VISUAL_ID != UnicornVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Element dst = link.getTarget();
			Element src = link.getSource();
			result.add(new UnicornLinkDescriptor(src, dst, link,
					UnicornElementTypes.Relation_4001,
					RelationEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<UnicornLinkDescriptor> getIncomingTypeModelFacetLinks_Relation_4001(
			Entity target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != UnicornPackage.eINSTANCE
					.getRelation_Target()
					|| false == setting.getEObject() instanceof Relation) {
				continue;
			}
			Relation link = (Relation) setting.getEObject();
			if (RelationEditPart.VISUAL_ID != UnicornVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Element src = link.getSource();
			result.add(new UnicornLinkDescriptor(src, target, link,
					UnicornElementTypes.Relation_4001,
					RelationEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<UnicornLinkDescriptor> getOutgoingTypeModelFacetLinks_Relation_4001(
			Entity source) {
		Element container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Element) {
				container = (Element) element;
			}
		}
		if (container == null) {
			return Collections.emptyList();
		}
		LinkedList<UnicornLinkDescriptor> result = new LinkedList<UnicornLinkDescriptor>();
		for (Iterator<?> links = container.getElements().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof Relation) {
				continue;
			}
			Relation link = (Relation) linkObject;
			if (RelationEditPart.VISUAL_ID != UnicornVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Element dst = link.getTarget();
			Element src = link.getSource();
			if (src != source) {
				continue;
			}
			result.add(new UnicornLinkDescriptor(src, dst, link,
					UnicornElementTypes.Relation_4001,
					RelationEditPart.VISUAL_ID));
		}
		return result;
	}

}
